﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;

namespace ExpertMIS.Forums.UserControl
{
    public delegate void RegisterActionDelegate(object sender, bool success, string account, string failedReason);

    public partial class ucQuickRegister : System.Web.UI.UserControl
    {
        public event RegisterActionDelegate OnRegister;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            string errorInfo = string.Empty;

            ADMUserBroker broker = new ADMUserBroker();
            ADMUserEntity user = new ADMUserEntity();

            user.Account = txID.Text;
            user.Email = txEMail.Text;
            user.Enable = true;
            user.IsAdmin = false;
            user.IsExpert = false;
            user.IsPassed = true;
            user.Name = txName.Text;
            user.Password = txPassword.Text;
            user.Tel = txPhone.Text;

            if (!broker.RegisterUser(user, ref errorInfo))
            {
                lblErrInfo.Text = errorInfo;
                lblErrInfo.Visible = true;

                if (OnRegister != null) OnRegister(this, false, user.Account, string.Empty);
            }
            else
            {
                if (OnRegister != null) OnRegister(this, true, string.Empty, errorInfo);
            }
        }
    }
}